package org.example.domain.strategy.service.rule.chain.impl;

import lombok.extern.slf4j.Slf4j;
import org.example.domain.strategy.repository.IStrategyRepository;
import org.example.domain.strategy.service.rule.chain.AbstractLogicChain;
import org.example.domain.strategy.service.rule.chain.factory.DefaultChainFactory;
import org.example.types.common.Constants;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;


/**
 * @Author: PengHao
 * @CreateTime: 2025-07-15  15:34
 * @Description: TODO
 */
@Slf4j
@Component("rule_blacklist")
public class BackListLogicChain extends AbstractLogicChain {
    @Resource
    private IStrategyRepository strategyRepository;

    @Override
    public DefaultChainFactory.StrategyAwardVO logic(String userId, Long strategyId) {
        log.info("抽奖责任链-黑名单开始 userId: {} strategyId: {} ruleModel: {}", userId, strategyId, ruleModel());

        //查询规则配置
        String ruleValue = strategyRepository.queryStrategyRuleValue(strategyId,ruleModel());
        if(ruleValue == null || ruleValue.isEmpty()){
            return next().logic(userId,strategyId);
        }
        String[] splitRuleValue = ruleValue.split(Constants.COLON);
        int awardId = Integer.parseInt(splitRuleValue[0]);

        // 黑名单过滤
        String[] userBlackIds = splitRuleValue[1].split(Constants.SPLIT);
        for (String userBlackId : userBlackIds) {
            if (userId.equals(userBlackId)) {
                return DefaultChainFactory.StrategyAwardVO.builder()
                        .logicModel(ruleModel())
                        .awardId(awardId)
                        .awardRuleValue("0.01,1")
                        .build();
            }
        }

        log.info("抽奖责任链-黑名单放行 userId: {} strategyId: {} ruleModel: {}", userId, strategyId, ruleModel());
        return next().logic(userId,strategyId);
    }


    @Override
    protected String ruleModel() {
        return DefaultChainFactory.LogicModel.RULE_BLACKLIST.getCode();
    }
}
